// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using ColorCode.Common;

namespace ColorCode.Compilation.Languages;

public class Php : ILanguage
{
    public string Id
    {
        get { return LanguageId.Php; }
    }

    public string Name
    {
        get { return "PHP"; }
    }

    public string CssClassName
    {
        get { return "php"; }
    }

    public string FirstLinePattern
    {
        get
        {
            return null;
        }
    }

    public IList<LanguageRule> Rules
    {
        get
        {
            return
                       [
                           new LanguageRule(
                               @"/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/",
                               new Dictionary<int, string>
                                   {
                                       { 0, ScopeName.Comment },
                                   }),
                           new LanguageRule(
                               @"(//.*?)\r?$",
                               new Dictionary<int, string>
                                   {
                                       { 1, ScopeName.Comment },
                                   }),
                           new LanguageRule(
                               @"(#.*?)\r?$",
                               new Dictionary<int, string>
                                   {
                                       { 1, ScopeName.Comment },
                                   }),
                           new LanguageRule(
                               @"'[^\n]*?(?<!\\)'",
                               new Dictionary<int, string>
                                   {
                                       { 0, ScopeName.String },
                                   }),
                           new LanguageRule(
                               @"""[^\n]*?(?<!\\)""",
                               new Dictionary<int, string>
                                   {
                                       { 0, ScopeName.String },
                                   }),
                           new LanguageRule(
                               // from http://us.php.net/manual/en/reserved.keywords.php
                               @"\b(abstract|and|array|as|break|case|catch|cfunction|class|clone|const|continue|declare|default|do|else|elseif|enddeclare|endfor|endforeach|endif|endswitch|endwhile|exception|extends|fclose|file|final|for|foreach|function|global|goto|if|implements|interface|instanceof|mysqli_fetch_object|namespace|new|old_function|or|php_user_filter|private|protected|public|static|switch|throw|try|use|var|while|xor|__CLASS__|__DIR__|__FILE__|__FUNCTION__|__LINE__|__METHOD__|__NAMESPACE__|die|echo|empty|exit|eval|include|include_once|isset|list|require|require_once|return|print|unset)\b",
                               new Dictionary<int, string>
                                   {
                                       { 1, ScopeName.Keyword },
                                   }),
                       ];
        }
    }

    public bool HasAlias(string lang)
    {
        return lang.ToLower() switch
        {
            "php3" or "php4" or "php5" => true,
            _ => false,
        };
    }

    public override string ToString()
    {
        return Name;
    }
}
